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
function delay(delayedFunction, time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
try { | |
resolve(delayedFunction()); | |
} catch (error) { | |
reject(error.message); | |
} | |
}, time); | |
}); |
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
console.log('1 | print immediately'); | |
delay(() => '2 | print after one second', 1000) | |
.then(result => console.log(result)); |
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
console.log('1 | print immediately'); | |
delay(() => { | |
throw new Error('This is a fake error.'); | |
return '2 | print after one second'; | |
}, 1000) | |
.then(result => console.log(result)) | |
.catch(error => console.log(error)); |
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
console.log('1 | print immediately'); | |
delay(() => '2 | print me after one second', 1000) | |
.then(result => { | |
console.log(result); | |
delay(() => '3 | print me one second after line 2', 1000) | |
.then(result => { | |
console.log(result); | |
/* ...CALLBACK HELL... */ | |
}); | |
}); |
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
function delay(delayedFunction, time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
try { | |
resolve(delayedFunction()); | |
} catch (error) { | |
reject(error.message); | |
} | |
}, time); | |
}); |
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
async function main() { | |
console.log('1 | print immediately'); | |
console.log(await delay(() => '2 | print after one second', 1000)); | |
} | |
main(); |
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
async function main() { | |
console.log('1 | print immediately'); | |
console.log(await delay(() => '2 | print after one second', 1000)); | |
console.log(await delay(() => '3 | print one second after line 2', 1000)); | |
console.log(await delay(() => '4 | print one second after line 3', 1000)); | |
console.log(await delay(() => '5 | print one second after line 4', 1000)); | |
} | |
main(); |
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
delay(() => '[RETURN VALUE]', 1000) | |
.then(result => { | |
console.log(result); // [RETURN VALUE] in result | |
}); |
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 result = await delay(() => '[RETURN VALUE]', 1000); | |
console.log(result); // [RETURN VALUE] in result |
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
delay(() => '[RETURN VALUE 1]', 1000) | |
.then(result => { | |
console.log(result); // [RETURN VALUE 1] in result | |
delay(() => '[RETURN VALUE 2]', 1000) | |
.then(result => { | |
console.log(result); // [RETURN VALUE 2] in result | |
}); | |
}); |