From 002e0c079f14b48bec32cf424055ad743f26881a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A4rt=20Kalmo?= Date: Sat, 8 Apr 2017 09:17:59 +0300 Subject: [PATCH] added promises exercise --- express-examples/express2.js | 2 ++ promises.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 promises.js diff --git a/express-examples/express2.js b/express-examples/express2.js index 21dbefc..3fac856 100644 --- a/express-examples/express2.js +++ b/express-examples/express2.js @@ -3,6 +3,8 @@ const express = require('express'); const app = express(); +app.use(express.static('./')); + app.get('/api/contacts', getContacts); app.get('/api/contacts/:id', getContact); diff --git a/promises.js b/promises.js new file mode 100644 index 0000000..7f3fe75 --- /dev/null +++ b/promises.js @@ -0,0 +1,30 @@ + +var promise = Promise.resolve(1); // vs. Promise.reject(new Error('failed')); + +promise.then(result => { + console.log('1: ' + result); + + return getData(result); +}).then(result => { + console.log('2: ' + result); + + return 1; +}).then(result => { + // no return +}).then(result => { + throw new Error('sth bad happened'); +}).then(result => { + // skipped +}).catch(error => { + // process error + // throw error + // return 1 +}).then(result => { + // executed if catch + // does not throw +}); + +function getData() { + return Promise.resolve('some data'); + // return Promise.reject(new Error('no data')); +}