Last active
August 5, 2023 23:50
-
-
Save lffg/0c160f986893be34976b0c8b9230a24e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function query(label) { | |
console.log(`(${label}) created 'lazy promise', but not started yet`); | |
return { | |
then(r) { | |
console.log(`(${label}) started!`); | |
// ... process query | |
// "return" them: | |
r([{ data: `result for ${label}` }]); | |
}, | |
}; | |
} | |
// won't execute, it's lazy: | |
query(1); | |
console.log(); | |
// will execute: | |
const results = await query(2); | |
console.log("(2) got results:", results); | |
console.log(); | |
// will also execute: | |
query(3).then((results) => { | |
console.log("(3) got results:", results); | |
}); | |
// program output: | |
// | |
// (1) created 'lazy promise', but not started yet | |
// | |
// (2) created 'lazy promise', but not started yet | |
// (2) started! | |
// (2) got results: [ { data: 'result for 2' } ] | |
// | |
// (3) created 'lazy promise', but not started yet | |
// (3) started! | |
// (3) got results: [ { data: 'result for 3' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment