0

I know how to do core callbacks and functions but I would like this use case:

   loadCSSFunction('/stylesheets/swiper.css',function (e){

        Execute code
        //Do some jquery call to a plugin for example

    });

How do I construct the loadCSSFunction to be able to form these functions, so once the action has taken place (injecting CSS into head (which I am fine with)) I can then execute something that relies on that CSS injection.

Update: Please note the CSS element of this is not relevant, its just part of the example and not relevant to the question. If I did want to inject it async etc I will do.

Function call:

   doSomethingWithFile('/folder/file.file',function (e){

        Execute code

    });

Function:

doSomethingWithFile: function(file){
      var mything = 'thing'+file;

    }
5
  • the function simply passes its callback parameter as the callback argument of whatever it's calling to perform the load. Commented Feb 9, 2017 at 22:58
  • If you show how loadCSSFunction performs the CSS loading, we can advise how to modify it. Commented Feb 9, 2017 at 23:00
  • stackoverflow.com/questions/5537622/… Commented Feb 9, 2017 at 23:02
  • It is not a duplicate @Barmar. IT may have CSS in there, but that was just used as the example information. Its just a sample use case. The core concept solution is what I am after Commented Feb 10, 2017 at 1:24
  • I've reopened. Please add the code of loadCSSFunction so we can understand what it's doing and show where to put the callback. Commented Feb 10, 2017 at 1:30

1 Answer 1

1

Just define your function with a callback parameter.

eg.

function loadCSSFunction(url, callback) {
   //do you CSS loading.
   //we have now loaded lets call the callback
   var e = 'something to pass to callback';
   callback(e);
}

Your do something with file, is really not much different to your CSSloader.

doSomethingWithFile: function(file, callback){
  var mything = 'thing'+file;
  //do what you want to do with file, 
  //again it doesn't matter if it's async / sync.
  //just call the callback when done..
  //eg below, readFileContents might be an async function
  readFileContents(file, function (err, data) {
    //normally async callbacks usually pass an err param
    //you should normally check for this too.
    if (err) callback(err); //if we have error pass it along.
    else callback(null, data); //here were sending our result back
  });
}

And now we can use our doSomethingWithFile..

doSomethingWithFile('thefile.txt', function (err, data) {
  if (err) console.error(err);
  else console.log(data);
});
Sign up to request clarification or add additional context in comments.

12 Comments

It's likely that the CSS loading stuff will be asynchronous, so he needs to call his callback in the async loader's callback.
Indeed but, without the rest of the CSS loading he said he can do, it's a little bit awkward to show this yet.
Indeed, that's why we should wait for him to clarify the question before trying to answer.
No, I answered the question. He wanted to know how to use the callback.. It's done.. How to handle Async operations is a different question..
He said he wants to call the callback after the CSS is loaded. The correct answer depends on whether it's loaded synchronously or asynchronously. Your answer only works for synchronous loading.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.