簡體   English   中英

如何在 node.js 中沿 .then 鏈傳遞承諾數據?

[英]How to pass promise data down the .then chain in node.js?

我試圖通過 Promise 鏈傳遞 readFile 的數據。

它能夠讀取,但是當我將讀取的數據傳遞給 writeFile 時,它​​收到未定義並寫入文件:

這是輸出:

node promise.js 
In fileFunction:
promiseResolveCallback():  File found!
readFromFileCallback: 11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111


writeToFileCallback: undefined

我的代碼:

var objFs   = require( 'fs' )

function promiseResolveCallback( arg )
{
    console.log("promiseResolveCallback(): ", arg)
}

function promiseRejectionCallback( arg )
{
    
    console.log("promiseRejectionCallback(): ", arg)
    throw -999;
}

function writeToFileCallback( argPromise )
{
    objFs.writeFile( 'new.js',
                     argPromise.data, 
                     ( argError ) => 
                     { 
                        if( argError ) 
                            throw argError 
                        else 
                            console.log( "writeToFileCallback: Written in new.js!" ) 
                     } 
                   )
}

function readFromFileCallback()
{
    return new Promise( (resolve, reject ) =>
                        {
                            objFs.readFile( 'new.js', ( argError, argData ) =>
                                                        {
                                                            if( argError )
                                                            {
                                                                reject( argError )
                                                                console.log( "readFromFileCallback: " + argError )
                                                            }
                                                            else
                                                            {
                                                                resolve( "read file \n")
                                                                console.log( "readFromFileCallback: " + argData )
                                                            }
                                                        }
                                          )
                        }
                     )  
}

function fileFunction()
{                        
     console.log("In fileFunction:")
     return new Promise( ( resolve, reject ) =>
                       {
                            objFs.open( 'new.js', 
                                        'r', 
                                        ( argError, argFD ) =>
                                        {
                                            if( argError )
                                            {
                                                reject( new Error("File not found!"), 1000);
                                            }
                                            else
                                            {
                                                resolve("File found!", 1000);
                                            }
                                        }
                                      )
                       }
                     );
} 

var objPromise = fileFunction()
objPromise.then( promiseResolveCallback, promiseRejectionCallback )
       .then( readFromFileCallback )
       .then( writeToFileCallback )

objPromise.catch( ( argError ) =>
                {
                    console.error( argError )
                } 
             )


                   
                    
                  

我對 Promises 的這個話題很陌生。 但我認為您需要在 readFromFileCallback 中使用 argData 來解決。

您可以使用 fs.Promises(默認情況下從 10.0 開始)進行讀寫。 例子

const fs = require("fs");

async function writeInput(message) {
  try {
    await fs.promises.writeFile("new.txt", message, "utf8");
    console.log("well done");
  } catch (err) {
    console.log(err);
  }
}

writeInput("Put your args data here");

async function readInput() {
    const secret = await fs.promises.readFile("new.txt","utf8");
    console.log(secret);
  }
  
  readInput();

輸出

well done
Put your args data here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM