added promises exercise

This commit is contained in:
Märt Kalmo 2017-04-08 09:17:59 +03:00
parent e488ae6eec
commit 002e0c079f
2 changed files with 32 additions and 0 deletions

View File

@ -3,6 +3,8 @@
const express = require('express'); const express = require('express');
const app = express(); const app = express();
app.use(express.static('./'));
app.get('/api/contacts', getContacts); app.get('/api/contacts', getContacts);
app.get('/api/contacts/:id', getContact); app.get('/api/contacts/:id', getContact);

30
promises.js Normal file
View File

@ -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'));
}