简体   繁体   中英

Error: Future resolved more than once

Everything works fine when I run my angular app for the 1st time on the browser.However, I have gotten the below errors messages when I refreshed the page.

/.../node_modules/fibers/future.js:248

  throw new Error('Future resolved more than once'); ^ Error: Future resolved more than once at Object.Future.return (/.../node_modules/fibers/future.js:248:10) at IncomingMessage.<anonymous> (/.../server.js:38:34) at IncomingMessage.emit (events.js:95:17) at IncomingMessage.<anonymous> (_stream_readable.js:765:14) at IncomingMessage.emit (events.js:92:17) at emitReadable_ (_stream_readable.js:427:10) at emitReadable (_stream_readable.js:423:5) at readableAddChunk (_stream_readable.js:166:9) at IncomingMessage.Readable.push (_stream_readable.js:128:10) at HTTPParser.parserOnBody [as onBody] (http.js:143:22) 

I have the following code in my server.js

var Fiber = require('fibers/future');  
var future = new Fiber;

function getNodeList(method){  
    ...  
    ...  
    ...  
    var req = https.request(options, function(res) {  
        var nodesList;  
        if (res.statusCode === 200) {  
            res.on('data', function(d) {  
                // process.stdout.write(d);  
                if (Buffer.isBuffer(d)) {  
                    nodesList = JSON.parse(d.toString('utf-8'));  
                    future.return(nodesList); <---Error Happen Here 
                }  
            });  
        } else {  
            console.log("ERROR");  
        }  
    });  
}  
app.post('/viewNodeList', function(req, res) {  
    var object = getNodeList("GET");  
    console.log("this One!");  
    res.status(200).send(object);  
}.future());  

The res object is a readable stream , which can receive its data in multiple chunks, with each provided in a separate 'data' event .

You'll want to also listen for the 'end' event , which will only occur once at most per stream, to know when all of the chunks have been received.

    if (res.statusCode === 200) {  
        let chunks = [];
        let totalLength = 0;

        res.on('data', function(chunk) {  
            totalLength += chunk.length;
            chunks.push(chunk);
        });  

        res.on('end', function () {
            var body = Buffer.concat(chunks, totalLength);
            nodeList = JSON.parse(body.toString());
            future.return(nodesList);
        });
    } // ...

You'll also want to create a new Future for each use of getNodeList() .

var Fiber = require('fibers/future');

function getNodeList(method){
    var future = new Future;

    // ...
}

By creating it at the top of the script, only one Future will exist for every request. And, once it's set as completed by the first request, it can't be reused for others after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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