0

I need to wait for the necessary changes in the object to finish the function and return the result to the caller. But I could not think of anything better than using "setInterval". I think I'm doing something wrong.

async checkLockBox ({ state, dispatch, commit }, lockBox) {
    if (lockBox.layers === 0) {
      commit('lockBox', null);
      return lockBox.result;
    }
    await commit('lockBox', lockBox);
    return new Promise((resolve) => {
      setInterval(async () => {
        if (state.lockBox !== null) {
          if (state.lockBox.layers === 0) {
            await resolve(state.lockBox.result);
            await commit('lockBox', null);
          }
        }
      }, 100);
    });
  },
Clara Oswald
  • 226
  • 1
  • 7
  • 1
    How many times should the function passed to `setInterval()` be called? Until the conditions are met? – guest271314 Jan 23 '18 at 02:29
  • See [multiple, sequential fetch() Promise](https://stackoverflow.com/questions/38034574/multiple-sequential-fetch-promise/) – guest271314 Jan 23 '18 at 02:31
  • @guest271314 Yes, it is necessary to wait until the conditions are met. – Clara Oswald Jan 23 '18 at 02:36
  • 1
    I guess you just want `while (state.lockBox) { /* do something */ await delay(100); }` – Bergi Jan 23 '18 at 02:36
  • `See multiple, sequential fetch() Promise` I don't quite understand how can apply the example given by you. I know and I use such a possibility, but I do not know how to use it in this case. – Clara Oswald Jan 23 '18 at 02:38
  • The code at the linked question schedules a repeated call to the same function which stores the results of each call in an array until a condition is met, then returns the array containing the results of N calls to the function. What is the purpose of returning a value from `checkLockBox()`? – guest271314 Jan 23 '18 at 02:40
  • "checkLockBox" is required to handle locks coming from the server. On removal of locks the user will receive a key with which help it will be possible to make operation which is protected from casual implementation. – Clara Oswald Jan 23 '18 at 02:47
  • 1
    @Bergi - what is `delay`? – Jaromanda X Jan 23 '18 at 02:48
  • @JaromandaX The [usual](https://stackoverflow.com/a/951057/1048572): `function delay(t) { return new Promise(r => setTimeout(r, t)); }` (I forgot to link it in my comment above, thanks for the reminder) – Bergi Jan 23 '18 at 02:56
  • I was asking for a friend, @ Bergi :p – Jaromanda X Jan 23 '18 at 02:57
  • You can use an async iterator to persistently await asynchronous code, handle errors and continue processing the calls, break, throw or return from the generator, see this [Answer](https://stackoverflow.com/a/48349837/) at [Run multiple recursive Promises and break when requested](https://stackoverflow.com/questions/48349721/run-multiple-recursive-promises-and-break-when-requested) – guest271314 Jan 23 '18 at 02:57
  • @Bergi cc//JaromandaX FWIW, `while ( await g.next().then(({value, done}) => {console.log(value, done); return !done}) )` at the link at previous comment was the pattern that was attempting to implement at [Why does assignment using await within while expression not retain assigned value?](https://stackoverflow.com/questions/47171474/why-does-assignment-using-await-within-while-expression-not-retain-assigned-valu) – guest271314 Jan 23 '18 at 03:01
  • @guest271314 Uh, don't do that. – Bergi Jan 23 '18 at 03:01
  • @Bergi Already did it. And tested the code through at least 50 versions. We have the ability to `return`, `throw` or `break` at any time. Though the concept is based on a `Proxy` for `Map` `.set` which stores the arguments, and a single command, or setting `done` to `true`, which stops or "cancels" the `Promise` "chain", though each call is awaited. Was trying to create a JavaScript quantum computer; not necessarily having a state that is only `1` or `0`; or at least a state that can be read which immediately reflect the actual state as the reader becomes a part of the transition of states. – guest271314 Jan 23 '18 at 03:02
  • @Bergi Was able to catch and handle errors at each line, and decide whether to stop the iteration or continue. The testing by this user alone does not mean the code is without issues from another perspective. Though the pattern is capable of achieving the requirement described by OP. – guest271314 Jan 23 '18 at 03:11
  • @Bergi `while (o.value.size && !o.value.done) {` should be `while (o.value.size && !o.done) {` – guest271314 Jan 23 '18 at 03:22
  • @guest271314 Please avoid this discussion here in the comments. That approach (I don't dare calling it a "solution") is overcomplicated and only confuses the OP. – Bergi Jan 23 '18 at 03:23

0 Answers0