如何通过 request-promise-native 获得自然解决的承诺
How to get a promise to resolve naturally with request-promise-native
我在 node.js 上使用 request-promise-native
模块。 API 我正在通过 GET 调用 returns 所需的数据。那很好用。
但是,当我尝试从函数中获取数据时,因为它前面带有 Async returns 承诺,所以我无法获得正确的语法。这是我尝试过的:
const request = require('request-promise-native');
async function usdToEos () {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
const response = await request(options)
.then(response => {
console.log(response)
return (1 / response.data.priceUsd)
})
.catch(error => {
console.log('\nCaught exception: ' + error);
})
}
var usdToEosMul = usdToEos()
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
如何使返回值成为...数据...(1 / response.data.priceUsd)。这在 ... console.log(response) ... 中可见,但在变量 usdToEosMul 中不可见。
the function which because it is preceded with async
returns a promise
看来您几乎已经回答了您的问题。您将不得不在您的呼叫站点等待该承诺:
usdToEos().then(usdToEosMul => {
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
}).catch(error => {
console.log('\nCaught exception: ' + error)
})
function usdToEos() {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
return request(options).then(response => {
console.log(response)
return (1 / response.data.priceUsd)
})
}
或
;(async function() {
try {
const usdToEosMul = await usdToEos()
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
} catch(error) {
console.log('\nCaught exception: ' + error)
}
}())
async function usdToEos() {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
const response = await request(options)
console.log(response)
return (1 / response.data.priceUsd)
}
我在 node.js 上使用 request-promise-native
模块。 API 我正在通过 GET 调用 returns 所需的数据。那很好用。
但是,当我尝试从函数中获取数据时,因为它前面带有 Async returns 承诺,所以我无法获得正确的语法。这是我尝试过的:
const request = require('request-promise-native');
async function usdToEos () {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
const response = await request(options)
.then(response => {
console.log(response)
return (1 / response.data.priceUsd)
})
.catch(error => {
console.log('\nCaught exception: ' + error);
})
}
var usdToEosMul = usdToEos()
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
如何使返回值成为...数据...(1 / response.data.priceUsd)。这在 ... console.log(response) ... 中可见,但在变量 usdToEosMul 中不可见。
the function which because it is preceded with
async
returns a promise
看来您几乎已经回答了您的问题。您将不得不在您的呼叫站点等待该承诺:
usdToEos().then(usdToEosMul => {
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
}).catch(error => {
console.log('\nCaught exception: ' + error)
})
function usdToEos() {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
return request(options).then(response => {
console.log(response)
return (1 / response.data.priceUsd)
})
}
或
;(async function() {
try {
const usdToEosMul = await usdToEos()
console.log('\n' + 'USD multiplier to convert to EOS' + '\n')
console.log(usdToEosMul)
} catch(error) {
console.log('\nCaught exception: ' + error)
}
}())
async function usdToEos() {
const options = {
method: 'GET'
,uri: 'https://api.coincap.io/v2/assets/eos'
,json: true
}
const response = await request(options)
console.log(response)
return (1 / response.data.priceUsd)
}