Skip to content

Refactoring from fetch to XMLHttpRequest should not break consumers of my library #22

Open
@bakkot

Description

@bakkot

This was discussed at some length on Matrix starting around here, but I want to capture it on this tracker so it doesn't get lost.

I think that it is not reasonable to ask every user, even those who are not using async contexts themselves, to know that Promise.prototype.then is a fundamentally different kind of thing than other mechanisms of scheduling callbacks, like XMLHttpRequest.prototype.onload.

That is, if I am not myself using async contexts, I absolutely should not have to know that a normally transparent refactoring like going from

function makeRequestThen(callback) {
  fetch(url).then(x => x.text()).then(result => callback(null, result), err => callback(err))
}

to

function makeRequestThen(callback) {
  const req = new XMLHttpRequest();
  req.addEventListener('load', () => callback(null, req.responseText));
  req.addEventListener('error', err => callback(err));
  req.open('GET', url);
  req.send();
}

is in fact a breaking change, because the former preserves the context for callback and the latter does not.

So either Promise.prototype.then should not automatically wrap its argument in the current context, or we should require of hosts that any mechanism for scheduling callbacks which might run in a later tick or turn should automatically wrap their argument in the current context.

(Or I'm misunderstanding how this whole proposal works, which is still possible.)

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