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 timeout(ms) { | |
return new Promise((resolve, reject) => { | |
setTimeout(reject, ms); | |
}) | |
} | |
Promise.race([readFilePromise('index.html'), timeout(1000)]) | |
.then(data => console.log(data)) | |
.catch(e => console.log("Timed out after 1 second")) | |
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
Promise.resolve() | |
.then(_ => api.getItem(1)) | |
.then(item => { | |
item.amount++; | |
return api.updateItem(1, item); | |
}) | |
.catch(e => { | |
console.log('failed to get or update item'); | |
}) |
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
Promise.resolve() | |
.then(_ => api.getItem(1)) | |
.catch(e => api.createItem(1, {amount: 0})) | |
.then(item => { | |
item.amount++; | |
return api.updateItem(1, item); | |
}) | |
.catch(e => { | |
console.log('failed to update item'); | |
}) |
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
api.getItem(1) | |
.then(item => { | |
item.amount++; | |
api.updateItem(1, item) | |
.then(update => { | |
api.deleteItem(1) | |
.then(deletion => { | |
console.log('done!'); | |
}) | |
}) |
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
api.getItem(1) | |
.then(item => { | |
item.amount++; | |
return api.updateItem(1, item); | |
}) | |
.then(update => { | |
return api.deleteItem(1); | |
}) | |
.then(deletion => { | |
console.log('done!'); |
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
api.getItem(1) | |
.then(item => { | |
item.amount++; | |
api.updateItem(1, item); | |
}) | |
.then(update => { | |
return api.deleteItem(1); | |
}) | |
.then(deletion => { | |
console.log('done!'); |
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 readFileAndMaybeLock(filename, createLockFile) { | |
let promise = Promise.resolve(); | |
if (createLockFile) { | |
promise = promise.then(_ => writeFilePromise(filename + '.lock', '')) | |
} | |
return promise.then(_ => readFilePromise(filename)); | |
} |
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 readFileAndMaybeLock(filename, createLockFile) { | |
let promise = Promise.resolve(); | |
if (createLockFile) { | |
promise.then(_ => writeFilePromise(filename + '.lock', '')) | |
} | |
return promise.then(_ => readFilePromise(filename)); | |
} |
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
let p = Promise.resolve('a'); | |
p.then(_ => 'b'); | |
p.then(result => { | |
console.log(result) // 'a' | |
}) | |
let q = Promise.resolve('a'); | |
q = q.then(_ => 'b'); | |
q = q.then(result => { | |
console.log(result) // 'b' |
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
let p = Promise.resolve(); | |
p.then(_ => {throw new Error("whoops!")}) | |
p.then(_ => { | |
console.log('hello!'); // 'hello!' | |
}) | |
let q = Promise.resolve(); | |
q = q.then(_ => {throw new Error("whoops!")}) | |
q = q.then(_ => { | |
console.log('hello'); // We never reach here |