异步承诺问题

时间:2017-10-02 08:53:24

标签: javascript asynchronous promise

我试图从查询字符串中获取匹配实体的列表。我使用s3存储我的对象。

问题在于解决“主要承诺”的承诺。是ansych只在给出setTimeOut时返回数组。否则它返回[undefined, undefined...]

我的代码如下:

getEntities: (type, offset, limit, query) => {
    return new Promise((resolve, reject) => {

        seekKeys(type, offset, limit, query)
            .then((response) => {

                let entities = [];


                if (response.hasBeenFiltered) { // This is not used yet. 
                    for (let i = offset; i < response.keys.length && i < offset + limit; i++) {
                        entities.push(matchInstance(instance))
                    }

                    resolve(entities)

                } else { // here is where my issue's at.
                    console.log("Keys found: " + response.keys.length)
                    parseQuery(type, query)
                        .then((conditions) => {

                            let matches = response.keys.map((key) => {
                                readEntity(key).then((instance) => {
                                    logger.debug(instance); // logs instances.
                                    matchInstance(instance, conditions)
                                        .then((isMatch) => {
                                            logger.debug("isMatch", isMatch) // logs true/false ?
                                            if (isMatch) {
                                                entities.push(instance);
                                            }
                                        })
                                        .catch((err) => {
                                            logger.error("Failed to match entity: ", err)
                                        })
                                })
                                    .catch((err) => {
                                        logger.error("Failed to read entity: ", err)
                                    })
                            });
                            /*
                            Promise.resolve(matches)
                                .then(() => {
                                    setTimeout(() => {
                                        logger.debug("Match count:", entities.length);
                                        logger.debug("Matching entities:", entities) // logs result of entities
                                    }, 5000)
                                    //resolve(entities)
                                })
                            */
                            Promise.resolve(matches)
                                .then(() => {
                                    logger.debug("Match count:", entities.length);
                                    logger.debug("Matching entities:", entities) // logs [undefined, undefined ....]
                                    resolve(entities)
                                })

                        })
                        .catch((err) => {
                            console.error("Failed to parse query: ", err)
                        });
            })
    })
}`

格式有点破碎。我很清楚为什么。 如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:2)

let matches = response.keys.map((key) => {
    // the mapping function:
    readEntity(key).then((instance) => {

映射函数似乎不返回值。这将创建一个带有未定义元素的映射数组。

let matches = response.keys.map((key) => {
    return readEntity(key).then((instance) => {
      // mapping function
通过填写matches承诺来阅读和处理实体,

可能会更好。等待履行承诺可以通过Promise.all完成,所以

 Promise.all(matches).then( ... process the entities array

更有可能工作
Promise.resolve(matches).then( ... process entities

除非matches是待处理的承诺,否则不会等待任何异步 - 但如果是这样,则在调用then之前不需要解决它。

请注意代码缩进令人困惑,因此检查控制台是否存在错误,以便在处理时查看matches是否在范围内 - 我无法看到它是。