How to fix the return type of the last promise in a chain?

James McLachlan

I want to return the last promise in a chain as the result of a function. I'm using TypeScript 1.7 and native, ES6 promises.

I tried this, but TS considers the first promise as the return value (the Uint8Array), not the CryptoKey from importKey. I've seen JavaScript examples that make me think the last promise is actually the return value, so maybe TS is just confused?

private getKey(): Promise<Uint8Array> {
    return localforage.getItem<Uint8Array>("key")
        .then<Uint8Array>((derivedKeyData) => {
            return crypto.subtle.importKey("raw", derivedKeyData, { name: "AES-GCM" }, false, ["decrypt"]);
        });
}

How do I return the inner promise as the result of the function? Or at least convince TS that's what's really happening without making the function's return type "any"?

Simon H

I had a look at your code on http://www.typescriptlang.org/Playground and the issue seems to be that the inbuilt resulting type of importKey is 'any' enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return promise.map (bluebird) inside a promise chain?

From Dev

How to call Iter::chain and what will be the return type?

From Dev

How to chain a RSVP promise and return the original reject/success functions?

From Dev

Promise: Ignore Catch and Return to Chain

From Dev

Promise catch() order in complex promise return chain

From Dev

how to break promise chain

From Dev

How to chain a Promise 'then' with 'all'

From Dev

How to break a promise chain?

From Java

How does Promise chain work when one callback doesn't return any promise?

From Dev

How can I release a resource in a nodeJS Q promise-chain and return a promise?

From Dev

how to return the last type of a variadic template?

From Dev

Chain Scala Futures return type

From Dev

Chain optionals with void return type

From Dev

How to break promise chain in Mongoose

From Dev

How to call this promise chain right

From Dev

AngularJS : How to flatten this Promise chain?

From Dev

how to pass parameters in promise chain

From Dev

How can I bypass the rest of the 'then's in a promise chain, like a traditional 'return' statement?

From Dev

How do I wrap a return type with a promise in TypeScript?

From Dev

How to infer return type of Promise based on function arguments?

From Dev

How to fix invalid method declaration; return type required?

From Dev

Using Promise as return type in Flow

From Dev

How to fix the promise anti pattern - Ghost Promise?

From Dev

bluebird promise each not return the last result

From Dev

Chain of promise

From Dev

How to return a failed promise?

From Dev

difference in how to return a promise?

From Dev

how to return a nested promise?

From Dev

How to return a promise?

Related Related

HotTag

Archive