Move onerror/onload logic into main function.
Move onerror/onload logic into main function.

This allows better reject handling, e.g. rejecting exceptions which are not Error objects.

blueimp/JavaScript-Load-Image,blueimp/JavaScript-Load-Image