Unpromisify pattern v5 - its hard to get this right

This commit is contained in:
Mitra Ardron 2019-02-22 18:44:52 +11:00
parent 5ef753c328
commit 1ecc34bba7
2 changed files with 3 additions and 1 deletions

View File

@ -130,3 +130,4 @@ See [Dweb document index](./DOCUMENTINDEX.md) for a list of the repos that make
* 0.1.35: package update (note wont work with latest versions of yjs or uglify) * 0.1.35: package update (note wont work with latest versions of yjs or uglify)
* 0.1.36: Made httptools accessable at Transports.httptools so it doesnt have to be separately 'require'd * 0.1.36: Made httptools accessable at Transports.httptools so it doesnt have to be separately 'require'd
* unreleased: IPFS - dont stop it if we didnt start it (were stopping via API) * unreleased: IPFS - dont stop it if we didnt start it (were stopping via API)
* unreleased: Start move to unpromisify pattern v5

View File

@ -120,7 +120,8 @@ httptools.p_GET = function(httpurl, opts={}, cb) { //TODO-API rearranged and add
}; };
const prom = httptools.p_httpfetch(httpurl, init, {wantstream: opts.wantstream}); // This s a real http url const prom = httptools.p_httpfetch(httpurl, init, {wantstream: opts.wantstream}); // This s a real http url
//if (cb) { prom.then((res)=>cb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3 //if (cb) { prom.then((res)=>cb(null,res)).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v3
if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)).catch((err) => debug("Uncaught error %O",err)); } else { return prom; } // Unpromisify pattern v4 //if (cb) { prom.catch((err) => cb(err)).then((res)=>cb(null,res)).catch((err) => debug("Uncaught error %O",err)); } else { return prom; } // Unpromisify pattern v4
if (cb) { prom.then((res)=>{ try { cb(null,res)} catch(err) { debug("Uncaught error %O",err)}}).catch((err) => cb(err)); } else { return prom; } // Unpromisify pattern v5
} }
httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb httptools.p_POST = function(httpurl, opts={}, cb) { //TODO-API rearranged and addded cb
/* Locate and return a block, based on its url /* Locate and return a block, based on its url