es6-promise

how to correctly extract text from a pdf using pdf.js

这一生的挚爱 提交于 2019-11-30 14:44:38
I'm new to ES6 and Promise. I'm trying pdf.js to extract texts from all pages of a pdf file into a string array. And when extraction is done, I want to parse the array somehow. Say pdf file(passed via typedarray correctly) has 4 pages and my code is: let str = []; PDFJS.getDocument(typedarray).then(function(pdf) { for(let i = 1; i <= pdf.numPages; i++) { pdf.getPage(i).then(function(page) { page.getTextContent().then(function(textContent) { for(let j = 0; j < textContent.items.length; j++) { str.push(textContent.items[j].str); } parse(str); }); }); } }); It manages to work, but, of course, the

How do you use es6 promises today on frontend?

不问归期 提交于 2019-11-30 13:34:15
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 + require because i can't get require js working as well) ps: Don't wan't to use webpack. Is there any

Can I use other promise implementations in the Parse JavaScript SDK?

一个人想着一个人 提交于 2019-11-30 11:56:37
I am concerned about references I have seen to Parse using JQuery-compatible promises, as I have read that jQuery promises allow consumers to mutate the state of the promise . Is it possible to use another promise implementation that is known to be Promises/A+ compliant (e.g. the ECMAScript 6 implementation , or Bluebird ) with the Parse JavaScript SDK? Normally I would assume that this isn’t possible, but in v1.4.2 of the Parse JavaScript SDK, the implementation of Parse.Promise defines the property “_isPromisesAPlusCompliant” as false which is then checked in various functions within the

jQuery ajax with ES6 Promises

只谈情不闲聊 提交于 2019-11-30 11:21:29
问题 I am trying to make a post request via jQuery using an ES6 promise: I have a function: getPostPromise(something, anotherthing) { return new Promise(function(resolve, reject) { $.ajax({ url: someURL, type: 'post', contentType: 'application/json; charset=utf-8', data: JSON.stringify( something: something, anotherthing: anotherthing }), dataType: 'json', success: resolve, error: reject }); }); } and I call it like so: getPostPromise( 'someFooStuff', 'someBarStuff' ).then( function(returnedData)

Intellij Idea warning - “Promise returned is ignored” with aysnc/await

房东的猫 提交于 2019-11-30 10:42:36
I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller . I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me a warning saying that Promise returned from login() is ignored. The thing is I'm not even returning anything from the login() method. Here's the code - UserRouter.js router.post('/login', function (req, res, next) { userController.login(req, res); // I get the warning here }); UserController.js exports.login = async function (req, res) { try {

Cleanest way to handle custom errors with fetch & ES6 promise

我是研究僧i 提交于 2019-11-30 09:24:42
I am trying to intelligently handle the success/error responses from our API using fetch & ES6 promises. Here is how I need to handle response statuses: 204: has no json response, but need to treat as success 406: should redirect to sign in 422: has json for error message < 400 (but not 204): success, will have json >= 400 (but not 422): error, will not have json So, I am struggling with how to write this cleanly. I have some less than stellar code working right now that looks like this: fetch() .then(response => checkStatus(response)) .then(parseJSON) //will throw for the 204 .then(data =>

Async / await assignment to object keys: is it concurrent?

有些话、适合烂在心里 提交于 2019-11-30 09:12:54
问题 I know that doing this: const resultA = await a() const resultB = await b() // code here Is effectively a().then( resultA => { b().then( resultB => { // code here }) }) Basically, a() runs then b() runs. I nested them to show that both resultA and resultB are in our scope; yet both function didn't run at once. But what about this: const obj = { result1: await a(), result2: await b() } do a() and b() run concurrently? For reference: const asyncFunc = async (func) => await func.call() const

Understanding promise.race() usage

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:24:06
问题 As far as I know, there are two options about promise: promise.all() promise.race() Ok, I know what promise.all() does. It runs promises in parallel, and .then gives you the values if both resolved successfully. Here is an example: Promise.all([ $.ajax({ url: 'test1.php' }), $.ajax({ url: 'test2.php' }) ]) .then(([res1, res2]) => { // Both requests resolved }) .catch(error => { // Something went wrong }); But I don't understand what does promise.race() is supposed to do exactly? In other word

Creating a (ES6) promise without starting to resolve it

青春壹個敷衍的年華 提交于 2019-11-30 06:17:25
问题 Using ES6 promises, how do I create a promise without defining the logic for resolving it? Here's a basic example (some TypeScript): var promises = {}; function waitFor(key: string): Promise<any> { if (key in promises) { return promises[key]; } var promise = new Promise(resolve => { // But I don't want to try resolving anything here :( }); promises[key] = promise; return promise; } function resolveWith(key: string, value: any): void { promises[key].resolve(value); // Not valid :( } It's

How to check if a Promise is pending [duplicate]

守給你的承諾、 提交于 2019-11-30 06:04:39
This question already has an answer here: How can I synchronously determine a JavaScript Promise's state? 19 answers I have this situation in which I would like to know what the status is of a promise. Below, the function start only calls someTest if it is not running anymore (Promise is not pending). The start function can be called many times, but if its called while the tests are still running, its not going to wait and returns just false class RunTest { start() { retVal = false; if (!this.promise) { this.promise = this.someTest(); retVal = true; } if ( /* if promise is resolved/rejected or