-
-
Save AutoSponge/4d8d06790f9dd6b51264 to your computer and use it in GitHub Desktop.
handle promise or value
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
const getUser = async $id => { | |
const id = await Promise.resolve($id); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve({id}), 100); | |
}); | |
} | |
const asyncId = new Promise((resolve, reject) => { | |
setTimeout(() => resolve(5), 100); | |
}); | |
getUser(asyncId).then(::console.log) | |
getUser(10).then(::console.log) //=> resolves and logs first |
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
const getUser = async (...args) => { | |
const [id] = await Promise.all(args); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve({id}), 100); | |
}); | |
} | |
const asyncId = new Promise((resolve, reject) => { | |
setTimeout(() => resolve(5), 100); | |
}); | |
getUser(asyncId).then(::console.log) | |
getUser(10).then(::console.log) //=> resolves and logs first |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment