gpt4 book ai didi

javascript - Promise join 向链中添加新的函数调用

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:44 24 4
gpt4 key购买 nike

我正在尝试加载和解析文件,但在调用两个函数并返回 promise 的结果时遇到了一些问题。我正在使用 Bluebird promise 。以下代码按预期工作:

run = function (filePath) {
return Promise.join(
fs.readFileAsync(filePath, 'utf8')
.then(parseFile.parse.bind(null, 'userKey')),
users.getUsersAsync(usersObj)
.then(users.modifyRec.bind(null, process.env.users))
).then(function (args) {
return runProc('run', args[0], args[1]);
....

我将parseFile.parse 函数分为两个方法,parseFile.parseparseFile.getPropparseFile.getProp 应该获取 parseFile.parse 的输出并返回 parseFile.parse 在方法被拆分之前返回的内容。这是我尝试同时使用这两个函数的尝试:

run = function (filePath) {
return Promise.join(
fs.readFileAsync(filePath, 'utf8')
.then(parseFile.parse.bind(null, 'userKey'))
.then(parseFile.getProp.bind(null,'key')),
users.getUsersAsync(usersObj)
.then(users.modifyRec.bind(null, process.env.users))
).then(function (args) {
return runProc('run', args[0], args[1]);
....

但它不起作用。我在这里做错了什么?

更新

var ymlParser = require('yamljs');
var ymlObj;

parse = function ( data) {
"use strict";
if (!ymlObj) {
ymlObj = ymlParser.parse(data);
}
return ymlObj;
};

getProcWeb = function () {
return ymlObj.prop.web;
};

module.exports = {
parse: parse,
getProp: getProp
};

最佳答案

Promise.join 不会返回数组,在您的情况下 - args[]。Promise.all 将返回一个数组。

因此,在您的情况下,您应该将 Promise.join 语法更改为

    Promise.join(
fs.readFileAsync(filePath, 'utf8')
.then(parseFile.parse.bind(null, 'userKey'))
.then(parseFile.getProp.bind(null,'key')),
users.getUsersAsync(usersObj)
.then(users.modifyRec.bind(null, process.env.users))
,function(argsOne,argsTwo){
return runProc('run', argsOne, argsTwo);
}));

或者使用 Promise.all

   Promise.all([promise1, promise2]).then(function(args){
return runProc('run', args[0], args[1]);
});

关于javascript - Promise join 向链中添加新的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32167120/

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