怪我咯2017-04-17 15:34:07
(err,res,body)=>{} is a callback, right?
It should be written like this
(err,res)=>{
if(err){
throw err
}else{
return (
//对res进行操作
)
}
}
res should be the data returned by the background. I don’t know the format, so I can only write an idea.
高洛峰2017-04-17 15:34:07
// callback
function rqst(callback) {
request({}, (err, res, body) => {
if (err) return callback(err);
callback(null, body);
})
}
rqst(function(err, body) {
// body
})
// promise
function rqst() {
return new Promise(resolve, reject) => {
request({}, (err, res, body) => {
if (err) reject(err);
resolve(body)
})
}
}
rqst.then(body => {
// body
}).catch(e => {
})