Add implementation for $.when()

This is extracted directly from jQuery.