Skip to content

Instantly share code, notes, and snippets.

@geoffreydhuyvetters
Created April 18, 2016 11:03
Show Gist options
  • Save geoffreydhuyvetters/44fd975c67eb29b3ed1490c4d4fce06c to your computer and use it in GitHub Desktop.
Save geoffreydhuyvetters/44fd975c67eb29b3ed1490c4d4fce06c to your computer and use it in GitHub Desktop.
Promises demo
(() => {
const delay = (time=1000) => {
return new Promise((resolve, reject) => {
if(typeof time !== 'number') return reject(new Error('wrong type'));
setTimeout(() => {
return resolve(`waited ${time}ms`);
}, time);
});
};
const init = () => {
delay(1000);
delay(1000);
delay(1000);
};
const init2 = () => {
delay(1000)
.then((message) => {
console.log(message)
return delay(1000)
})
.then(() => delay('test'))
.then(() => console.log('done'))
.catch(e => console.log(e));
};
init2();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment