【发布时间】:2017-11-29 14:14:27
【问题描述】:
我在理解承诺方面有些问题。你能帮我吗:
我正在尝试使用node-geocoder 库。
所以,这是一个函数,它应该返回谷歌地图上某个点的经纬度数组。
import NodeGeocoder from 'node-geocoder'
export default function getLocation () {
const geocoder = NodeGeocoder({
provider: 'google',
})
const point = geocoder.geocode('29 champs elysée paris', (error, response) => {
return [response[0].latitude, response[0].longitude]
})
return point
}
之前的代码应该可以用于这个测试:
import getLocation from './index'
test('This is a test', () => {
expect(getLocation()).toEqual([48.8698679, 2.3072976])
})
测试失败,我收到以下错误消息:
Expected value to equal:
[48.8698679, 2.3072976]
Received:
{"fulfillmentValue": undefined, "isFulfilled": false, "isRejected": false, "rejectionReason": undefined}
【问题讨论】:
-
如何打开尚未发货的包裹? Promise 就像一个发货确认,只是你正在等待的东西的占位符。你所能做的就是等待承诺兑现/包裹交付。
.then()你可以随心所欲,随心所欲。
标签: javascript ecmascript-6 promise geocoding google-geocoder