简体   繁体   English

function 内的 Promise

[英]Promise within a function

This code does not execute the promise of testAuthentication or pinFileToIPFS and i am curious if this is a node concept i am not familiar of.此代码不执行 testAuthentication 或 pinFileToIPFS 的 promise,我很好奇这是否是我不熟悉的节点概念。

function uploadToPinata(filename) {
    const pinata = pinataSDK(process.env.PINATA_KEY, process.env.PINATA_SECRET_KEY);
    pinata.testAuthentication().then((result) => {
        //handle successful authentication here
        console.log(result);
    }).catch((err) => {
        //handle error here
        console.log(err);
    });
    
    const readableStreamForFile = fs.createReadStream(filename);
    const options = {
        pinataMetadata: {
            name: "NYC_NFT_TESTING",
        },
        pinataOptions: {
            cidVersion: 0
        }
    };

    pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
        //handle results here
        console.log(result);
    }).catch((err) => {
        //handle error here
        console.log(err);
    });
}

is there a problem with this code using a promise within a function?在 function 中使用 promise 的代码是否存在问题? I attemped to make the function async but that did not help.我试图使 function 异步,但这并没有帮助。 This code works just fine outside of a function but not within one.此代码在 function 之外工作得很好,但不在一个内。

"does not execute the promise" : this is a strange phrase. “不履行承诺” :这是一个奇怪的短语。 Promises are objects. Promise 是对象。 They don't execute -- they are created .它们不执行——它们是被创建的。 And your function really creates them.你的 function 真的创造了它们。 However, it does not do much when these promises are resolved.但是,当这些承诺得到解决时,它并没有多大作用。

The problem is that uploadToPinata will execute all of the synchronous code and return.问题是uploadToPinata将执行所有同步代码并返回。 But then there are still a promises pending.但是仍然有一个未决的承诺。 Although you have console.log that will be executed once the relevant promise has resolved, there is no signal to the caller of uploadToPinata that these promises have resolved.尽管您有console.log将在相关的 promise 解决后执行,但没有信号通知uploadToPinata的调用者这些承诺已解决。

It is probably easiest to use async and await :使用asyncawait可能是最简单的:

async function uploadToPinata(filename) {
    const pinata = pinataSDK(process.env.PINATA_KEY, process.env.PINATA_SECRET_KEY);
    const result = await pinata.testAuthentication();
    //handle successful authentication here
    console.log("authentication result", result);
    const readableStreamForFile = fs.createReadStream(filename);
    const options = {
        pinataMetadata: { name: "NYC_NFT_TESTING" },
        pinataOptions: { cidVersion: 0 }
    };

    const result2 = await pinata.pinFileToIPFS(readableStreamForFile, options);
    //handle results here
    console.log("pinFileToIPFS result", result2);
}

The caller of this function will now receive a promise, so it should probably want to do something when that promise has resolved:此 function 的调用者现在将收到 promise,因此当 promise 已解决时,它可能想要做一些事情:

uploadToPinata(filename).then(() => {
    console.log("authentication and file operation done")
}).catch(error => console.log("error", error));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM