我正在尝试使用'request'模块在node.js中创建一个请求,但是它异步工作。如何在“ something1”之前在控制台上显示“ something2”,我该如何解决?有什么办法可以使其同步工作?
const request = require('request')
getSomething: function() {
var options = {
method: 'GET',
url: '...',
headers: {
Authorization: 'Bearer ...'
},
json: true
};
request(options, (err, res, body) => {
//code
console.log('something1')
}
})
console.log('something2')
},
尝试这个
getSomething: function() {
var options = {
method: 'GET',
url: '...',
headers: {
Authorization: 'Bearer ...'
},
json: true
};
var promise = new Promise((resolve, reject) => {
request(options, (err, res, body) => {
//code
console.log('something1')
resolve()
}
})
})
promise.then(() => {
console.log('something2')
})
},
我希望能成功!也请在此处查看MDN,以获取更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句