2017-04-21 18 views
-1

我很抱歉詢問已發佈多次的問題,但我無法理解如何修改示例/解決方案/教程以我的代碼。我受限於修改別人的代碼,無法從頭開始。使用Nodejs執行異步圖像操作並且無法理解承諾或回調來確定何時完成所有操作

我正在使用nodejs v6.10,儘管讀了許多文章和維基,但異步代碼處理對我來說很難實現。

我試圖確定所有操作完成的時間,我相信承諾對我來說是正確的。我無法弄清楚如何讓它正常工作,但我沒有再收到任何警告或錯誤。

我認爲我最大的問題是我的圖像處理函數不會返回任何東西,我試圖強迫它們沒有成功。

這裏是我的基本代碼:

var finished; 
main(); 

function main() { 
    do stuff... 
    fs.readFile(JSON,...) { 
     finished = theApp(JSON); 
}); 

Promise.all(finished).then(function(x, y) { 
    var total = x * y; 
    console.log("completed: " + total + " at " + Date.now()); 
     }).catch(function() { 
      console.log("failed."); 
     }); 
} 

function theApp(JSON) { 
    do stuff... 
    for $loop (1..100) { 
     do JSON stuff... 
     resizeImages(JSONparameters, image); 
    } 

    for $loop2 (1..100) { 
     do JSON stuff... 
     finished = function() { 
      return manipulateImages(JSONparameters, image); 
     } 
    } 
} 

function resizeImages(JSONparameters, image) { 
    do stuff... 
    for $i (1..100) { 
     sharp(image) 
      .resize(x, y) 
      .toFile(output) 
    } 
} 

function manipulateImages(JSONparameters, image) { 
    return new Promise(function(resolve, reject) { 
     do stuff... 
     for $i (1..100) { 
      sharp(image) 
       .blur() 
       .toFile(output) 
     } 
    }); 
}  

我意識到這是一個很大的循環,但是這是我的約束對於現在的架構。我也意識到我只是在操縱圖像步驟中承諾。這是因爲調整操作在操作開始之前完成。

在此配置中,manipulateImages函數被調用很多次,但沒有任何輸出。如果我去掉代碼中的「返回新的Promise」包裝,它可以正常工作。但是,我不知道如何返回任何可以傳回main的東西,等待promises.all()返回解析。

有人可以請教我如何讓我console.log所有操作完成的確切時間?謝謝。

+1

你的manupulate圖像函數返回一個promise,但是在你的循環中,你只是將完成的變量設置爲一個返回所述操作圖像函數的函數。我沒有看到你在哪裏叫什麼。您可以將調用manupilate images函數的結果添加到數組中,然後在所有循環運行後,使用promises數組調用Promise.all(),並在其成功函數中調用Promise.all(),當所有的promise都返回時。 – Ken

+1

你的「架構」有很多問題。尚未分配時使用'finished'。如果沒有,則使用'theApp'的返回值。你在兩個不同的地方分配了「完成」。 'Promise.all'需要一個數組,但是你(在一種情況下)將一個Promise分配給'finished'。你正在創造一個承諾,但你永遠不會解決或拒絕它。您可能在使用Promise的同時使用可能不是異步的函數... – jcaron

+0

如果您使用的是Promise.all,請確保完成的變量是數組類型的並且在其中有承諾。這是我用代碼看到頭頂的唯一東西。現在看起來完成是一個函數,而不是一個數組。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all –

回答

0

我認爲你對Promises的基本理解並不那麼完備,而且你的解決方案已經足夠複雜了,這讓你更難理解Promise的核心以及它們是如何工作的。想想這樣:

// List of images 
let images = [ 
    'image1.jpg', 
    'image2.jpg', 
    'image3.jpg', 
] 

// Array to store promises 
let promises = [] 

// Loop through your images 
for (const image of images) { 

    // Create a new promise, adding it to the array 
    promises.push(new Promise((resolve, reject) => { 

    // Do your resizing, manipulation, etc 
    sharp(image) 
     .resize() 
     .blur() 
     .toFile(output, function(err, data) { // Assuming this toFile is an async operation and uses a callback 

     // After all the resizing, manipulation and file saving async functions are complete, resolve the Promise 
     resolve() 
     }) 


    }) 
} 

// Wait until all promises in the promises array are resolved 
Promise.all(promises) 
    .then((){ 
    console.log('image resizing and manipulation is complete') 
    }) 

瞭解承諾的最好方法是,在承諾內,你做所有的異步的東西。處理回調等等。在Promise中做任何你需要做的異步操作。當你完成,resolve它。這是什麼表明諾言是完整的。

Promise.all正在等待promises數組中的所有Promise在它運行之前完成它的.then()方法。

相關問題