我試圖使用node-geocoder npm包來獲取位置的緯度和經度的一些對象。返回JavaScript承諾拒絕履行價值
請幫我理解JavaScript的承諾。
下面是一個例子代碼:
import nodeGeocoder from 'node-geocoder'
function getLocation (str) {
// Selecting Google as provider
const geocoder = nodeGeocoder({
provider: 'google',
})
return geocoder.geocode(str)
.then(response => {
// I want to return latitude an longitude
return [response[0].latitude, response[0].longitude]
})
.catch(error => {
console.log(`Geocoder Error: ${ error }`)
})
}
export default getLocation
大二這是一個測試(玩笑框架):
import getLocation from './index'
test('Checking',() => {
expect(getLocation('29 champs elysée paris')).toEqual([48.8698679, 2.3072976])
})
當我試圖用這個測試我只是得到承諾地位{"fulfillmentValue": undefined, "isFulfilled": false, "isRejected": false, "rejectionReason": undefined}
。
但我需要得到只是承諾解決的結果。我該怎麼做?
我不希望編輯測試
您無法返回尚未到達的結果。等待你必須履行的承諾。 – Bergi