Toggle navigation
Genres
Frontend (2)
JavaScript (6)
Database (2)
Linux Server (3)
Web Apps (4)
Misc (4)
Search
List
New Tutorials
Last Modified Tutorials
</>
Code examples
code
smiko
javascript
nodejs
promises
50bluebird_newPromise.js
50bluebird_newPromise.js
/** * basic usage of bluebird */ var Promise = require("bluebird"); //defining an async function var clAsync = function () { var P = new Promise(function (resolve, reject) { console.log('Write from function'); resolve('ok'); reject('not ok'); }); /* { "isFulfilled": true, "isRejected": false, "fulfillmentValue": "ok" } */ console.log(JSON.stringify(P, null, 2)); return P; }; console.log(JSON.stringify(clAsync, null, 2)); //undefined //executing promisified function clAsync() and adding new callback with then method clAsync() .then(function (returnedVal) { console.log('THEN 1 - fulfillmentValue= ' + returnedVal); return {one: 1, two: 'drugi'}; //returning to next then }) .then(function (returnedVal) { console.logs('THEN 2 - fulfillmentValue= ' + returnedVal.one + returnedVal.two); // return new Error('Namjerna greška'); --doesn't work throw new Error('Namjerna greška', 'someFile.js', 152); }) //catch type errors .catch(TypeError, function (e) { console.error(e.stack); // console.error(e.message); }) //catch all other errors .catch(function (e) { console.error(e.message); // console.error(e.stack); });
Reload page
Preview
W3C validation
Edit Code
JS Console