Last active
December 28, 2018 09:40
-
-
Save YSMull/5cc09b4761b625d8cd2fd5932c6427a7 to your computer and use it in GitHub Desktop.
let M = await N 的解糖模式应该是: new Promise((resolve, reject)=>{resolve(N)}).then(M => {...})
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yyy = async function() { | |
return 123; | |
} | |
let multipleAwait = async function () { | |
let a = await yyy(); | |
let b = await yyy(); | |
console.log(a+b); | |
} | |
let multipleAwaitP = function () { | |
new Promise(function (resolve, reject) { | |
resolve(yyy()) | |
}).then(a => { | |
new Promise(function (resolve, reject) { | |
resolve(yyy()) | |
}).then(b => { | |
console.log(a+b) | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let xxx = async function() { | |
return 'done'; | |
} | |
let f1 = function () { | |
let p = xxx(); | |
return p; | |
} | |
let f2 = async function () { | |
let p = xxx(); | |
return p; | |
} | |
let f3 = async function () { | |
let p = await xxx(); | |
return p; | |
} | |
let extractSugar = async function () { | |
// await f1() | |
let a1 = await xxx(); | |
// await f2() | |
let a2 = await new Promise(function(resolve, reject) { | |
resolve(xxx()); | |
}) | |
// await f3() wrong | |
let a3 = await new Promise(function(resolve, reject) { | |
xxx().then(res => { | |
resolve(res) | |
}); | |
}); | |
// await f3() wrong | |
let _a3 = await new Promise(function (resolve, reject) { | |
new Promise(function (resolve1, reject1) { | |
xxx().then(res => { | |
resolve(res); | |
}) | |
}) | |
}) | |
// await f3() <-- bench closest to await f3() | |
let trueA3 = await new Promise(function (resolve, reject) { | |
resolve(new Promise(function (resolve1, reject1) { | |
xxx().then(res => { | |
resolve1(res); | |
}) | |
})) | |
}) | |
console.log(a1,a2,a3,_a3,trueA3) | |
} | |
// extractSugar() | |
let benchmark1 = async function () { | |
console.time('cost'); | |
for (let i = 0; i < 1000000; i++) { | |
// await f1(); // 174ms | |
// await f2(); // 226ms | |
// await f3(); // 376ms | |
} | |
console.timeEnd('cost') | |
} | |
// benchmark1() | |
let benchmark2 = async function () { | |
console.time('cost'); | |
for (let i = 0; i < 1000000; i++) { | |
// 170ms | |
// await xxx(); | |
// 265ms | |
// await new Promise(function(resolve, reject) { | |
// resolve(xxx()); | |
// }); | |
// 285ms | |
// await new Promise(function(resolve, reject) { | |
// xxx().then(res => { | |
// resolve(res) | |
// }); | |
// }); | |
// 322ms | |
// await new Promise(function (resolve, reject) { | |
// new Promise(function (resolve1, reject1) { | |
// xxx().then(res => { | |
// resolve(res); | |
// }) | |
// }) | |
// }) | |
// 375ms --- closest to await f3() | |
// await new Promise(function (resolve, reject) { | |
// resolve(new Promise(function (resolve1, reject1) { | |
// xxx().then(res => { | |
// resolve1(res); | |
// }) | |
// })) | |
// }) | |
} | |
console.timeEnd('cost') | |
} | |
benchmark2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment