Skip to content

Instantly share code, notes, and snippets.

@basselin
Created June 18, 2019 08:09
Show Gist options
  • Save basselin/e6d3c263956d8985a090f35af10ef39b to your computer and use it in GitHub Desktop.
Save basselin/e6d3c263956d8985a090f35af10ef39b to your computer and use it in GitHub Desktop.
Promise.js
(async () => {
let promise1 = new Promise((resolve, eject) => {
resolve('YES');
});
promise1.then( r => console.log(r) ); // YES
console.log(await promise1); // YES
const test2 = async () => {
return await promise1;
};
console.log(test2()); // Promise
test2().then( r => console.log(r) ); // YES
console.log(await test2()); // YES
const test3 = async () => {
const text = await promise1;
return text + ' ' + text;
};
console.log(test3()); // Promise
test3().then( r => console.log(r) ); // YES YES
console.log(await test3()); // YES YES
const concat = (val1, val2) => val1 + ' ' + val2;
const test4 = async () => {
const text = await promise1;
return concat(text, text);
};
console.log(test4()); // Promise
test4().then( r => console.log(r) ); // YES YES
console.log(await test4()); // YES YES
console.log(concat(await promise1, await promise1)); // YES YES
console.log(concat(promise1, promise1)); // [object Promise] [object Promise]
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment