Last active
July 21, 2018 05:38
-
-
Save snowman/fbabd9803ff06b57a58521d947eda1e1 to your computer and use it in GitHub Desktop.
Javascript promise
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
// http://jsfiddle.net/mkjh2ev5/1245/ | |
let cleanRoom = function() { | |
return new Promise(function(resolve, reject) { | |
resolve('Cleaned The Room'); | |
}); | |
}; | |
let removeGarbage = function(message) { | |
return new Promise(function(resolve, reject) { | |
reject(message + ' remove Garbage'); | |
}); | |
}; | |
let winIcecream = function(message) { | |
return new Promise(function(resolve, reject) { | |
resolve( message + ' won Icecream'); | |
}); | |
}; | |
Promise.all( | |
[cleanRoom(), | |
removeGarbage(), | |
winIcecream()] | |
).then(function (arr) { | |
console.log(arr) | |
}).catch(function (err) { | |
console.log(`Error: ${err}`); | |
}) | |
cleanRoom().then(function(result){ | |
console.log("Success") | |
return removeGarbage(result); | |
}).then(function(result){ | |
return winIcecream(result); | |
}).then(function(result){ | |
console.log('finished ' + result); | |
}).catch(function(error){ | |
console.log(`Error: ${error}`); | |
}) |
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
// https://www.youtube.com/watch?v=2d7s3spWAzo&list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84 | |
let addImg = (src) => { | |
let imgElement = | |
document.createElement("img") | |
imgElement.src = src | |
document.body.appendChild(imgElement) | |
} | |
// promise implementation | |
function loadImage(url) { | |
return new Promise((resolve, reject) => { | |
let image = new Image() | |
image.onload = function() { | |
resolve(image) | |
} | |
image.onerror = function() { | |
let message = | |
'Could not load image at ' + url | |
reject(new Error(message)) | |
} | |
image.src = url | |
}) | |
} | |
Promise.all([ | |
loadImage('images/cat1.jpg'), | |
loadImage('images/cat.jpg'), | |
loadImage('images/cat3.jpg'), | |
loadImage('images/cat4.jpg') | |
]).then((images) => { | |
console.log(images) | |
images.forEach(img => addImg(img.src)) | |
}).catch((error) => { | |
// handle error later | |
console.log(error) | |
}) | |
// callback implementation | |
function loadImageWithCallback(url, callback) { | |
let image = new Image() | |
image.onload = function () { | |
callback(null, image) | |
} | |
image.onerror = function () { | |
let message = | |
'Could not load image at: ' + url | |
callback(new Error(message)) | |
} | |
image.src = url | |
} | |
loadImageWithCallback('images/cat1.jpg', (error, img1) => { | |
if(error) throw error; | |
addImg(img1.src) | |
loadImageWithCallback('images/cat2.jpg', (error, img2) => { | |
if(error) throw error; | |
addImg(img2.src) | |
loadImageWithCallback('images/cat3.jpg', (error, img3) => { | |
if(error) throw error; | |
addImg(img3.src) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment