Javascript Promise tips
After using Promise for a while, I realized the followings might be good to know:
- The
executorfunction of a promise is executed immediately after creation. That means if you create a Promise object usingnew, the function you passed in (theexecutor) will run right after thenewstatement. 1
For example, in the following codes:var prom = new Promise((resolve, reject) => { console.log('1') setTimeout(() => { resolve('3') }, 3000) }) prom.then(console.log) console.log('2')
The output is:
1
2
3