Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Created November 4, 2016 11:29
Show Gist options
  • Save juanmaguitar/ce9b0ffb3492aa57f3da0f11fb561a6c to your computer and use it in GitHub Desktop.
Save juanmaguitar/ce9b0ffb3492aa57f3da0f11fb561a6c to your computer and use it in GitHub Desktop.
Promises Examples
const fs = require("fs")
fs.readFile('test.txt', 'utf-8', (err, contentFile) => {
setTimeout( ()=> {
contentFile = contentFile.toUpperCase();
fs.writeFile('output.txt', contentFile, (err) => {
console.log("file written")
})
}, 2000)
})
const fs = require("fs")
var readFilePromised = function(fileToRead) {
return new Promise( (resolve,reject) => {
fs.readFile(fileToRead, 'utf-8', (err, contentFile) => {
resolve(contentFile)
})
})
}
var delayedAction = function(contentFile) {
return new Promise( (resolve,reject) => {
setTimeout( ()=> {
contentFile = contentFile.toUpperCase();
resolve(contentFile)
})
})
}
var writeFilePromised = function(contentToWrite) {
return new Promise( (resolve,reject) => {
fs.writeFile('output.txt', contentToWrite, (err) => {
resolve("file written!")
})
})
}
function shoutOut( content ) {
console.log (content)
return content;
}
readFilePromised('test.txt')
.then( delayedAction )
.then( shoutOut )
.then( writeFilePromised )
.then( console.log )
var promise1 = new Promise(function (fulfill, reject) {
var msg = "1st Promise Resolved..."
setTimeout( function() { fulfill(msg)}, 3000)
});
var promise2 = new Promise(function (fulfill, reject) {
var msg = "and 2nd Promise Resolved to..."
setTimeout( function() { fulfill(msg)}, 5000)
});
var promise3 = new Promise(function (fulfill, reject) {
var msg = "and 3rd Promise also Resolved!"
setTimeout( function() { fulfill(msg)}, 10000)
});
// Your solution here
function showMsg (text) {
console.log(text);
}
/* Way 1 - Callback Promises Hell */
// promise1.then( function( msg ) {
// showMsg(msg);
// promise2.then( function( msg ) {
// showMsg(msg);
// promise3.then( function( msg ) {
// showMsg(msg);
// });
// })
// })
/* Way 2 - Returning Promises */
// promise1
// .then( function(msg) {
// showMsg(msg);
// return promise2;
// })
// .then( function(msg) {
// showMsg(msg);
// return promise3;
// })
// .then( function(msg) {
// showMsg(msg);
// })
/* Way 3 - Inproving Returning Promises */
// function showMsgEnhanced (text) {
// console.log(text);
// return (text)
// }
// promise1
// .then( showMsgEnhanced )
// .then (function(msg) {
// console.log ("prev Msg => " + msg)
// return promise2
// .then(showMsgEnhanced);
// })
// .then (function(msg) {
// console.log ("prev Msg => " + msg)
// return promise3
// .then(showMsgEnhanced);
// })
/* Way 4 - Inproving Returning Promises */
function showMsgEnhanced (text) {
console.log(text);
return (text)
}
function showPrevMsgAndDoSomethingPromised2 (msg) {
console.log ("prev Msg => " + msg)
return promise2
}
function showPrevMsgAndDoSomethingPromised3 (msg) {
console.log ("prev Msg => " + msg)
return promise3
}
promise1
.then( showMsgEnhanced )
.then( showPrevMsgAndDoSomethingPromised2 )
.then( showMsgEnhanced )
.then( showPrevMsgAndDoSomethingPromised3 )
.then( showMsgEnhanced );
function getSearchData( arrayAPI ) {
function createPromiseFromItem ( item ) {
var getUrl = 'https://api.spotify.com/v1/search?q='+ item +'&type=track';
return appFactory.apiResult( getUrl ); // returns a promise
}
function showConsole(data) {
console.log(data);
}
// ["aaaa", "vbbbbb", "cccccc"]
// [Promise, Promise, Promise ]
$q.all( arrayAPI.map(createPromiseFromItem) )
.then( showConsole );
}
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("operation completed!!")
},5000)
});
var promise2 = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("another operation completed!!")
},3000)
});
var allPromise = Promise.all([promise, promise2]);
var racePromise = Promise.race([promise, promise2]);
allPromise.then(function(data) {
console.log( data )
})
racePromise.then(function(data) {
console.log( data )
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment