执行.Then()承诺NodeJS [英] Execute .Then() promise NodeJS

查看:76
本文介绍了执行.Then()承诺NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用".then"调用Promise,以便可以继续在返回的Promise输出上执行其他功能.我已经确认正在使用的功能可以正常工作,但是在观看视频和其他SO示例之后,仍然很难使它正常工作.请参见以下代码段:

I am trying to figure out how to call a Promise using ".then" so I can continue performing other functions on the return Promise output. I have confirmed the function being used works as expected, but after watching videos and other SO examples am still having a hard time getting this working. See below snippet:

const fs = require('fs');
const JSZip = require('jszip');


const directoryFile = fs.readdirSync('./zipped');
//console.log(directoryFile);

var zip = new JSZip();
var dir = ('./zipped/');


const readZip = async () => {
    const promiseToken = new Promise((resolve, reject) => {
        fs.readFile((dir + directoryFile), function (err, data) {
            if (err) throw err;
            JSZip.loadAsync(data).then(function (zip) {
                const files = Object.keys(zip.files);
                console.log(files);
                files.forEach((files) => {
                    const pkgName = files.substring(files.indexOf("_", files.indexOf("_")) + 1, files.lastIndexOf("_"));
                    const fileExt = files.split('.').pop();
                    const pkgExtract = (pkgName + "." + fileExt);
                });
            })
        });
        return promiseToken;
    });
};


    console.log('Program Starts');

    readZip().then((promiseToken) => {
        console.log(promiseToken.join(','));
    });


    console.log('Program Ends');

我不断收到(node:1144376)UnhandledPromiseRejectionWarning:ReferenceError:初始化之前无法访问'promiseToken'"上面的代码块采用文件名数组,并循环遍历每个元素,提取文件名的一部分并将其合并我需要创建一个新文件名的每个元素名称.同样,该功能可以正常工作并经过测试.什么是行不通的,当我尝试通过".then()"调用"readZip()"的返回输出时.我需要使这部分工作,以便我可以继续执行脚本的其余部分.我是NodeJS和Java的新手,我将不提供任何帮助,因为我尝试过的所有视频或示例都无法正常工作.....

I keep getting "(node:1144376) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'promiseToken' before initialization" The above code block take an array of file names and loops through each element and extracts portions of the file name and joins the portions of each elements name that I need to create a new file name. Again, the function works and is tested. What is not working is when I try to call the return output of "readZip()" via ".then()". I need to do get this portion working so I can continue on with the rest of the script. I am new to NodeJS and Javascript and any assistance would be appreciated as none of the videos or examples I have attempted seem to be working.....

推荐答案

您似乎错过了一行,并且正在从promise的构造函数而不是从 readZip的定义中返回 promiseToken .因此, readZip 不返回任何内容,并且您会得到一个 undefined 值.

You seem to have missed a line, and are returning promiseToken from the promise's constructor instead of from the definition of readZip. Therefore, readZip returns nothing, and you get an undefined value.

将其移到正确的位置,就可以了:

Move it to the correct place and you should be fine:

const readZip = async () => {
    const promiseToken = new Promise((resolve, reject) => {
        fs.readFile((dir + directoryFile), function (err, data) {
            if (err) throw err;
            JSZip.loadAsync(data).then(function (zip) {
                const files = Object.keys(zip.files);
                console.log(files);
                files.forEach((files) => {
                    const pkgName = files.substring(files.indexOf("_", files.indexOf("_")) + 1, files.lastIndexOf("_"));
                    const fileExt = files.split('.').pop();
                    const pkgExtract = (pkgName + "." + fileExt);
                });
            })
        });
    });
    return promiseToken; // Moved here
};

这篇关于执行.Then()承诺NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆