$ npm install --save cc-promise-shim
const Promise = require('cc-promise-shim');
new Promise(function (resolve, reject) {
get('http://www.google.com', function (err, res) {
if (err) reject(err);
else resolve(res);
});
}).then((data)=>{
console.log(data);
}).catch((err)=>{
console.error(err);
});
Before all examples, you will need:
const Promise = require('cc-promise-shim');
This creates and returns a new promise. fn
must be a function. The fn
function is passed two arguments:
-
resolve
should be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A). -
reject
should be called with a single argument. The returned promise will be rejected with that argument.
These methods are invoked by calling Promise.methodName
.
Promise.resolve(value)
Promise.reject(value)
Promise.all(array)
Promise.race(array)