Skip to content

Resolve all manual resolution requests by calling pretender.resolve() with no arguments? #306

Description

@ctaylor-nurx

So the docs are a little thin on the use case of manual async handlers (manual delay in the docs). I'm looking for an easier way to resolve all the manual requests that are sitting around waiting.

// register the handler as manually being resolved
pretender.get('/api/songs', songHandler, true);
fetch('/api/songs');
// then to resolve it, we need a reference to the request
pretender.resolve(request);

Right now I'm using this util function I wrote to do it:

function resolveManualRequests(pretender) {
  pretender.requestReferences.forEach((r) => {
    if (pretender.requiresManualResolution(r.request.method, r.request.url)) {
      pretender.resolve(r.request);
    }
  });
}

pulling a reference to the request itself to pass in can be a pain. It'd be great to be able to just manually resolve all standing requests. I think just calling pretender.resolve() with no arguments would be a clear way to achieve this. It'd be very easy to implement by just using this.requiresManualResolution(verb, path) and then recursing on itself with the request.

resolve: function resolve(request) {
  if (request) {
    for (let i = 0, len = this.requestReferences.length; i < len; i++) {
      let res = this.requestReferences[i];
      if (res.request === request) {
        res.callback();
        this.requestReferences.splice(i, 1);
        break;
      }
    }
  } else {
    this.requestReferences.forEach((request) => {
      if (this.requiresManualResolution(request.method, request.url)) {
        this.resolve(request);
      }
    });
  }
},

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions