added promises exercise
This commit is contained in:
parent
e488ae6eec
commit
002e0c079f
@ -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);
|
||||
|
||||
|
30
promises.js
Normal file
30
promises.js
Normal 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'));
|
||||
}
|
Loading…
Reference in New Issue
Block a user