Created
December 22, 2017 07:38
-
-
Save serinuntius/442b1bcf92e079df56d45848e2a6a632 to your computer and use it in GitHub Desktop.
async勉強会のやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const host = 'http://localhost:3000'; | |
const getBook = () => { | |
return new Promise((resolve, reject) => { | |
request.get(`${host}/books/1`, (err, res, body) => { | |
if (err) { | |
reject(err) | |
} | |
resolve(JSON.parse(body)); | |
}) | |
}); | |
}; | |
const postBook = (params) => { | |
return new Promise((resolve, reject) => { | |
request.post({url: `${host}/books`, form: params}, (err, res, body) => { | |
if (err) { | |
reject(err) | |
} | |
resolve(JSON.parse(body)); | |
}) | |
}); | |
}; | |
let result1, result2, result3, result4; | |
getBook() | |
.then(_data => { | |
result1 = _data; | |
result1.id = null; | |
return postBook(result1) | |
}) | |
.then(_data => { | |
result2 = _data; | |
return getBook() | |
}) | |
.then(_data => { | |
result3 = _data; | |
result3.id = null; | |
return postBook(result3) | |
}) | |
.then(_data => { | |
result4 = _data; | |
console.log(result1); | |
console.log(result2); | |
console.log(result3); | |
console.log(result4); | |
}) | |
.catch(err => { | |
console.log(err); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment