Giter Club home page Giter Club logo

Comments (5)

markmcd avatar markmcd commented on May 19, 2024

@brendankenny @samthor ?

from google-maps-services-js.

stephenfarrar avatar stephenfarrar commented on May 19, 2024

OK, I had a play with what things look like with promises, and it's much nicer. Then I had a play with what things look like using window.fetch, and think this also makes a LOT of sense.

  1. A lot of thought has gone into window.fetch and promises/A+, and so if our API looks like it, it will probably be well-design and pleasant to use. It will also feel familiar in years to come if window.fetch gets widespread adoption.
  2. It makes the implementation of our client library much easier to read.

I've been injecting the fetch function, so that the developer can supply their own fetch polyfill (for browser or node). That feels quite natural. You have inject your API key as well, so the injection step had to happen anyway.

Injecting the fetch function will also allow people to unit test against our client library. According to Michael Feathers, this is "the golden rule of API design" (97 Things Every Programmer Should Know, 2010).

from google-maps-services-js.

stephenfarrar avatar stephenfarrar commented on May 19, 2024

Another issue with Promises and window.fetch(): how do you cancel?
AFAICT, the community hasn't decided how this should be done.

This is pretty important for us, since the client library offers to rate-limit and automatically retry.

  • In my opinion, promises ought not be cancelled... that's an oxymoron. I'm not a fan of the back-pressure idea either. AND
  • The fetch spec doesn't allow cancelling today. BUT
  • Our client library request could be cancelled, because rate-limiting or retrying might mean we haven't yet called fetch().

Since our request function returns a promise, it will have to expose the cancelling mechanism via a revealing constructor. I imagine it looking like this:

var cancelGeocode;
googleMaps.geocode({address: 'Sydney Opera House'}, function(cancel) {
  cancelGeocode = cancel;
})
    .then(json)
    .then(function(response) {
      // do something
    });

// Meanwhile ...
if (weNoLongerNeedTheGeocode) {
  cancelGeocode();
}

Cancelling the request would reject the promise.

@juliaogris I'd appreciate your opinion too.

from google-maps-services-js.

stephenfarrar avatar stephenfarrar commented on May 19, 2024

I ought to mention too that calling cancel() can only be a hint. It doesn't guarantee that the Promise will be rejected.

from google-maps-services-js.

stephenmcd avatar stephenmcd commented on May 19, 2024

Closing this now since I think we've addressed everything here with the current state of the library.

from google-maps-services-js.

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.