Javascript Promise tips
After using Promise for a while, I realized the followings might be good to know:
- The
executor
function 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 thenew
statement. 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