es6-promise

Handling errors in express async middleware

巧了我就是萌 提交于 2019-12-01 06:11:17
I have an async middleware in express, because I want to use await inside it, to clean up my code. const express = require('express'); const app = express(); app.use(async(req, res, next) => { await authenticate(req); next(); }); app.get('/route', async(req, res) => { const result = await request('http://example.com'); res.end(result); }); app.use((err, req, res, next) => { console.error(err); res .status(500) .end('error'); }) app.listen(8080); The problem is that when it rejects, it doesn't go to my error middleware, but if I remove the async keyword and throw inside a middleware it does.

What are the advantages of Promises over CPS and the Continuation Functor/Monad?

霸气de小男生 提交于 2019-12-01 06:06:48
ES6 Promises ES6 Promises are finite state machines and thus require complex implementations. Beyond that the Promise/A+ spec comes with a lot of rough edges: overloaded then (map/chain) recursive flattening / then-able assimilation automatic lifting several subscribers (multicast) eager evaluation Multicast distribution and eager evaluation are among other things the reasons why ES6 promises cannot be cancelled. Additionally, we can't add our own layers of then-ables with specific features, because they are immediately assimilated by recursive flattening. I am pretty sure there were a lot of

What are the implications of the recursive joining of Promises in terms of Monads?

爱⌒轻易说出口 提交于 2019-12-01 04:12:42
问题 I know that Javascript's promises are technically neither functors nor monads in the sense of Haskell, because (among other things) they include a bind operation that falls back to map when a pure function is passed in (and thus has an ambiguous type) both the Promise constructor and resolve (aka return ) join nested promises recursively The first issue can easily be bypassed by always providing a function with the right type a -> Promise b . The second issue obviously violates the

What are the advantages of Promises over CPS and the Continuation Functor/Monad?

断了今生、忘了曾经 提交于 2019-12-01 03:04:18
问题 ES6 Promises ES6 Promises are finite state machines and thus require complex implementations. Beyond that the Promise/A+ spec comes with a lot of rough edges: overloaded then (map/chain) recursive flattening / then-able assimilation automatic lifting several subscribers (multicast) eager evaluation Multicast distribution and eager evaluation are among other things the reasons why ES6 promises cannot be cancelled. Additionally, we can't add our own layers of then-ables with specific features,

How do I catch ES6 Promise rejections and completely stop flow?

↘锁芯ラ 提交于 2019-11-30 23:34:44
Say I have 4 functions: runA() , runB() , runC() and runD() . Using ES6 promises, in a completely successful run, these would all be run one after another: runA() .then(runB) .then(runC) .then(runD) If runA or runB fail (reject or throw), I would like to call error1() and then completely stop the chain (not call runC or runD ). This makes me think I should add a single .catch() at the very end of the .then promise chain: runA() .then(runB) .then(runC) //won't get called if runA or runB throws .then(runD) //won't get called if runA or runB throws .catch(error1) But if runC fails, I would like

When Promise.then() hooks are called?

£可爱£侵袭症+ 提交于 2019-11-30 19:38:15
问题 I observe deferring of completeness notifications in Firefox's promises. Following assertion fails, because onFullfilled() is called too late * . var resolved = false; function onFullfilled() { resolved = true; log("Completed"); } Promise.resolve(true).then(onFullfilled); assert(resolved, "Promise completed promise should call resolution hook immediately."); When exactly onFullfilled() is guaranteed to be called on Promise resolution? * In my case "Completed" log message appears after test

Angular: Return Observable / ES6 Promise from FileReader

北城以北 提交于 2019-11-30 19:11:41
I was trying to return result from FileReader and I found this implementation. But since it is outdated, I'm wondering how to implement the same using ES6 Promises or Rx Observables . Below is my code with reference to the aforementioned link and it works as expected. import { Injectable } from '@angular/core'; import * as XLSX from 'xlsx'; import * as XLS from 'xlsx'; @Injectable() export class ExcelReaderService { constructor() { } importFromExcel(ev): JQueryPromise<any> { let deferred = $.Deferred(); let regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/; let workbook; let excelInJSON; if

How to deal with dangling promises

荒凉一梦 提交于 2019-11-30 15:50:21
问题 Oftentimes I want to call a promise and just let it run asynchronously instead of waiting for it. Like: ... some code ... fetchMyCount().then(count => { updateTheUI(count); }); ... more code ... Now this works great, but oftentimes I don't put a failure handler on the promise because that is pretty onerous for each one. That would be like putting a try {} catch {} on every one of my data fetches. The problem is, if it fails, it does so silently. It doesn't throw an exception that my window

Using native ES6 promises with MongoDB

六眼飞鱼酱① 提交于 2019-11-30 15:37:06
问题 I'm aware that the Node driver for Mongo can be promisified using external libraries. I was curious to see if ES6 promises could be used with MongoClient.connect , so I tried this (using Babel 5.8.23 to transpile): import MongoClient from 'mongodb'; function DbConnection({ host = 'localhost', port = 27017, database = 'foo' }) { return new Promise((resolve, reject) => { MongoClient.connect(`mongodb://${host}:${port}/${database}`, (err, db) => { err ? reject(err) : resolve(db); }); }); }

How do you use es6 promises today on frontend?

柔情痞子 提交于 2019-11-30 15:07:11
问题 I'm trying to use babel to compile file that contains es6 promises. I have installed babel-cli, babel-preset-es2015, babel-plugin-es6-promise. My .babelrc config is: { "presets": ["es2015"], "plugins": ["es6-promise"] } I got compiled js file with require() inside, but i don't want to use require at all. Is there any possibility of using es6 promises today on frontend side without require js? Please provide any link to es6 promises implementation sample with babel (or even with babel +