如图想在reqst函数内部返回body的内容,求指教~
人生最曼妙的风景,竟是内心的淡定与从容!
callback
(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.
// 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 => { })
callback
(err,res,body)=>{} is a callback, right?
It should be written like this
res should be the data returned by the background. I don’t know the format, so I can only write an idea.