gpt4 book ai didi

javascript - 将 Fluture Future 转换为民间故事结果

转载 作者:行者123 更新时间:2023-11-29 20:53:52 25 4
gpt4 key购买 nike

我有以下示例代码来获取 uuid:

const Result = require('folktale/result')
const Future = require('fluture')

Future.prototype.flatMap = Future.prototype.chain

const fetch = Future.encaseP(require('node-fetch'))
const message = err => `ERROR: ${err.message}`

function getUuid () {
return fetch('https://httpbin.org/uuid')
}

const future = getUuid()
.map(response => {
const responseJson = response.json.bind(response)
const getResponseBody = Future.encaseP(responseJson)
return getResponseBody()
})
.map(obj => obj.uuid)

const result = future.fold(Result.Error, Result.Ok).fork(x => x, x => x)
console.log(result) // [Function: Sequence$cancel]
result.map(
x => {
return message(x)
},
x => {
console.log(x)
return x
}
)

结果不是可映射类型,当我调用 result.map 时抛出错误:

TypeError: result.map is not a function

最佳答案

fork 执行 future 并期望您处理在 fork 参数中提供的回调中的结果,更重要的是返回 cancel 函数,不是 next Future,正如您所期望的那样,我在其结果上应用了 .map

将代码更改为:

const cancelToken = future
.fold(Result.Error, Result.Ok)
// if you wanna map result, do it here:
// .map(...)
.fork( // fork consumes the fluture and ends execution chain
ok => message(x),
err => { console.log(err); }
);
// call cancelToken() if you want to cancel it

关于javascript - 将 Fluture Future 转换为民间故事结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com