Yes, the non-overlapping way to write it IMO would be to isolate the rejection on errors to a function:
``` let watchError = (client) => new Promise((resolve, reject) => client.on("error", reject)); // as a plain promise function fetchish(){ let client = new Client; return Promise.race([watchError(client), client.get()]).then(JSON.parse)); } // as an async function async function fetchish(){ let client = new Client(); let result = await Promise.race([client.get(), watchError(client)]); return JSON.parse(result); } ``` On Wed, Feb 4, 2015 at 1:16 AM, Andrea Giammarchi < andrea.giammar...@gmail.com> wrote: > to be honest that looks more like an all-in (good old events in the house) > ... put a generator somewhere, please! > </ignore-me> > > I wonder if actually fetchish shouldn't be directly a Promise instead, > feels like overlapping. Just random thoughts, curious to see more mixed > async patterns in the real world > > Best Regards > > On Tue, Feb 3, 2015 at 8:26 PM, Kevin Smith <zenpars...@gmail.com> wrote: > >> I would think that a hypothetical `client.get` method would reject when >> an error occurred, so that you wouldn't need to attach an "error" handler. >> >> That said, you can also freely mix promises with await and async: >> >> async function fetchish() { >> >> let text = await new Promise((resolve, reject) => { >> >> let client = new Client; >> client.on("error", reject); >> resolve(client.get()); >> }); >> >> return JSON.parse(text); >> } >> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss