Skip to content

Instantly share code, notes, and snippets.

@maxidr
Last active August 3, 2017 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxidr/fadfea7503397fb545451d2a5b536ffd to your computer and use it in GitHub Desktop.
Save maxidr/fadfea7503397fb545451d2a5b536ffd to your computer and use it in GitHub Desktop.
Make xhr request that will be aborted if you call it again. Using mithril request
const merge = require('ramda/src/merge')
const m = require('mithril')
/*
@param {string} key - The unique key of the request
@param {Object} params - The same params used in m.request of mithril
@returns Promise
@link https://mithril.js.org/request.html
Example of use:
function searchUser(email){
return uniqueReq('searchUser', {
method: 'GET',
url: '/users?email=' + email
})
}
searchUser('john.doe@test.com').then(user => console.log(user))
Every time that you call the function searchUser, the previous request will be aborted.
*/
function uniqueReq(key, params){
if( ! uniqueReq.xhrs ){ uniqueReq.xhrs = {} }
if( uniqueReq.xhrs[key] ){ uniqueReq.xhrs[key].abort() }
return m.request(
merge(
params,
{ config: xhr => uniqueReq.xhrs[key] = xhr }
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment