2021年3月20日星期六

Eloquent Javascript (findInRemoteStorage) .then executing a function, but choosing not to return promise that resolves to its return value

This is from Eloquent JavaScript's Chapter 11 - Asynchronous Programming and all related code can be found in crow-tech.js and chapter/11_async.js

Can someone tell me what that comma after next() is doing? I don't mean what the comma operator does, but why structure it that way? The hope would be that one of these recursive calls to next() finally produces a valid 'value' - why is 'next(),next' necessary? So it executes next() again, but returns... just the 'next' function object - instead of a promise that resolves to the return value of next()(which sounds like what is actually needed?). Why? WHY? I am so confused.

I know it's hard to understand what's going on without taking a bunch of time, so if anyone has a suggestion of a a good way to answer questions about a specific book, let me know.

.then(value => value != null ? value : next(),        next);  

from:

function findInRemoteStorage(nest, name) {    let sources = network(nest).filter(n => n != nest.name);    function next() {      if (sources.length == 0) {        return Promise.reject(new Error("Not found"));      } else {        let source = sources[Math.floor(Math.random() *          sources.length)];        sources = sources.filter(n => n != source);        return routeRequest(nest, source, "storage", name)          .then(value => value != null ? value : next(),            next);      }    }    return next();  }  
https://stackoverflow.com/questions/66727704/eloquent-javascript-findinremotestorage-then-executing-a-function-but-choosi March 21, 2021 at 08:13AM

没有评论:

发表评论