Returns promise (only if promise.constructor == Promise)
Make a promise that fulfills to obj. in this situation.
| #!/bin/bash | |
| SOURCE_DIR=/usr/local/src | |
| SVN_VERSION=1.7.20 | |
| SVN_SRC_DIR=$SOURCE_DIR/subversion-$SVN_VERSION | |
| # Install dependencies | |
| sudo yum -y groupinstall "Development Tools" "Development Libraries" | |
| sudo yum -y install libxml2 libxml2-devel libxslt-devel openssl openssl-devel |
| #!/bin/bash | |
| # Get script path | |
| SOURCE_DIR=/usr/local/src | |
| SVN_VERSION=1.7.20 | |
| SVN_SRC_DIR=$SOURCE_DIR/subversion-$SVN_VERSION | |
| # Install dependencies | |
| sudo apt-get -y install build-essential libxml2 libxml2-dev libxslt1-dev openssl libssl-dev |
| Promise.all(arrayOfPromises).then(function(arrayOfResults) { | |
| //... | |
| }); | |
| /*****************************************************************/ | |
| get('story.json').then(function(response) { | |
| console.log("Success!", response); | |
| }, function(error) { | |
| console.log("Failed!", error); | |
| }); | |
| /************************************************/ | |
| getJSON('story.json').then(function(story) { | |
| return getJSON(story.chapterUrls[0]); | |
| }).then(function(chapter1) { | |
| console.log("Got chapter 1!", chapter1); | |
| }); | |
| /*************************/ | |
| var promise = new Promise(function(resolve, reject) { | |
| resolve(1); | |
| }); | |
| promise.then(function(val) { | |
| console.log(val); // 1 | |
| return val + 2; | |
| }).then(function(val) { | |
| console.log(val); // 3 | |
| }); |
| function get(url) { | |
| // Return a new promise. | |
| return new Promise(function(resolve, reject) { | |
| // Do the usual XHR stuff | |
| var req = new XMLHttpRequest(); | |
| req.open('GET', url); | |
| req.onload = function() { | |
| // This is called even on 404 etc | |
| // so check the status |
| promise.then(function(result) { | |
| console.log(result); // "Stuff worked!" | |
| }, function(err) { | |
| console.log(err); // Error: "It broke" | |
| }); |
| var promise = new Promise(function(resolve, reject) { | |
| // do a thing, possibly async, then… | |
| if (/* everything turned out fine */) { | |
| resolve("Stuff worked!"); | |
| } | |
| else { | |
| reject(Error("It broke")); | |
| } | |
| }); |