Git Product home page Git Product logo

Comments (5)

Nodonisko avatar Nodonisko commented on May 24, 2024

Try something like this:

let key2 = 'some-promise2';
this.cache.getItem(key2).catch(() => {
    return someOtherPromiseFunction().then(result => {
       return this.cache.saveItem(key1, result).then(() => result); // because method saveItem do not return saved data, only resolve promise when is data successfully saved
    });
}).then((data) => {
    console.log("Saved data2: ", data);
});
...

from ionic-cache.

Nodonisko avatar Nodonisko commented on May 24, 2024

But this good point, it should be added to lib as feature.

from ionic-cache.

vptcnt avatar vptcnt commented on May 24, 2024

Thanks. but the question was about chaining key1 and key2. because each set of promises are executed asynchronously.

from ionic-cache.

Nodonisko avatar Nodonisko commented on May 24, 2024

It's not related to library, but it's easy :)

let key1 = 'some-promise1';
let key2 = 'some-promise2';
this.cache.getItem(key1).catch(() => {
    return somePromiseFunction().then(result => {
        return this.cache.saveItem(key1, result).then(() => result);
    });
}).then((result1) => {
    console.log(result1)
    return this.cache.getItem(key2).catch(() => {
       return someOtherPromiseFunction().then(result => {
          return this.cache.saveItem(key2, result).then(() => result);
       });
    })
}).then((result2) => {
  console.log(result2)
});

or with nicer chaining

let key1 = 'some-promise1';
let key2 = 'some-promise2';
this.cache.getItem(key1)
.catch(() => {
    return somePromiseFunction()
})
.then(result => {
    return this.cache.saveItem(key1, result).then(() => result);
})
.then((result1) => {
    console.log(result1)
    return this.cache.getItem(key2).catch(() => {
       return someOtherPromiseFunction()
    })
}).then((result2) => {
  console.log(result2)
  return this.cache.saveItem(key2, result2)
});

from ionic-cache.

vptcnt avatar vptcnt commented on May 24, 2024

It works... thanks...

from ionic-cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.